unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls;
const
N=20000;
type
TForm1 = class(TForm)


    Image1: TImage;
    Timer1: TTimer;
    Label1: TLabel;
procedure FormCreate(Sender: TObject);
procedure ShakerSort (var mass: array of integer);
procedure AdvancedBubbleSort (var mass: array of integer);
procedure BubbleSort (var mass: array of integer);
procedure InsertionSort (var mass: array of integer);
procedure Heapify (t: integer; var mass:array of integer);
procedure HeapSort (var mass: array of integer);
procedure Timer1Timer(Sender: TObject);

private
{ Private declarations }
public
{ Public declarations }
end;


var
Form1: TForm1;
arr, arr1, arr2, arr3, arr4, arr5: array of integer;
i, t, t1, t2, t3, t4, t5, w, h, k : integer;
a:real;

implementation

{$R *.dfm}


procedure TForm1.ShakerSort (var mass: array of integer);
var
i, j: integer;
flag: boolean;
begin
for i:= 1 to n div 2 do
begin
flag:= true;
for j:= i-1 to N-i-1 do
if (mass[j] > mass[j+1]) then
begin
mass[j]:=mass[j]+mass[j+1];
mass[j+1]:=mass[j]-mass[j+1];
mass[j]:=mass[j]-mass[j+1];
flag:=false;
end;
for j:= N-i-1 downto i-1 do
if (mass[j] < mass[j-1]) then
begin
mass[j]:=mass[j]+mass[j-1];
mass[j-1]:=mass[j]-mass[j-1];
mass[j]:=mass[j]-mass[j-1];
flag:=false;
end;
if (flag) then exit;
end;
end;

procedure TForm1.AdvancedBubbleSort (var mass: array of integer);
var i, j: integer;
flag: boolean;
begin
for j:= N-2 downto 1 do
begin
flag:=true;
for i:= 0 to j do
begin
if (mass[i]>mass[i+1]) then
begin
mass[i]:=mass[i]+mass[i+1];
mass[i+1]:=mass[i]-mass[i+1];
mass[i]:=mass[i]-mass[i+1];
flag:=false;
end;
end;
if (flag=true) then
exit;
end;
end;

procedure TForm1.BubbleSort (var mass: array of integer);
var i, j: integer;
begin
for j:= N-2 downto 1 do
begin
for i:= 0 to j do
begin
if (mass[i]>mass[i+1]) then
begin
mass[i]:=mass[i]+mass[i+1];
mass[i+1]:=mass[i]-mass[i+1];
mass[i]:=mass[i]-mass[i+1];
end;
end;
end;
end;

procedure TForm1.InsertionSort (var mass: array of integer);
var i,j,k, t: integer;
begin
for i:= 1 to N-1 do
for j:= 0 to i-1 do
begin
if (mass[i]<mass[0]) then
begin
t:=mass[i];
for k:= i-1 downto 0 do
mass[k+1]:=mass[k];
mass[0]:=t;
break;
end;
if (mass[i]>=mass[j]) and (mass[i] <= mass[j+1]) then
begin
t:=mass[i];
for k:= i-1 downto j+1 do
mass[k+1]:=mass[k];
mass[j+1]:=t;
break;
end;
end;
end;

procedure TForm1.Heapify (t: integer; var mass:array of integer);
var a,b: integer;
begin
while (1=1) do
begin
a:=2000000;
b:=2000000;
if (2*t+1<n) then
a:=mass[2*t+1];
if (2*t+2<n) then
b:=mass[2*t+2];
if (a<=b) and (a<mass[t]) then
begin
mass[t]:=mass[t]+mass[2*t+1];
mass[2*t+1]:=mass[t]-mass[2*t+1];
mass[t]:=mass[t]-mass[2*t+1];
t:=t*2+1;
end else
begin
if (b<a) and (b<mass[t]) then
begin
mass[t]:=mass[t]+mass[2*t+2];
mass[2*t+2]:=mass[t]-mass[2*t+2];
mass[t]:=mass[t]-mass[2*t+2];
t:=2*t+2;
end else
break;
end;
end;
end;


procedure TForm1.HeapSort (var mass: array of integer);
var d, i: integer;
begin
d:=N;
for i:= d-1 downto 0 do
heapify (i, mass);
for i:= 1 to d-1 do
begin
mass[0]:=mass[d-1]+mass[0];
mass[d-1]:=mass[0]-mass[d-1];
mass[0]:=mass[0]-mass[d-1];
d:=d-1;
heapify (0, mass);
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
Randomize;
k:=1;
Setlength (arr1, N);
Setlength (arr2, N);
Setlength (arr3, N);
Setlength (arr4, N);
Setlength (arr5, N);
for i:= 0 to N-1 do
begin
arr1[i]:=random(100);
arr2[i]:=arr1[i];
arr3[i]:=arr1[i];
arr4[i]:=arr1[i];
arr5[i]:=arr1[i];
end;
w:=Image1.Canvas.TextWidth('advancedbubbleSort: 9999 ms');
h:=Image1.Canvas.TextHeight('bubbleSort: 9999 ms');
t:=GetTickCount;
BubbleSort (arr1);
t1:=GetTickCount-t;

t:=GetTickCount;
AdvancedBubbleSort (arr2);
t2:=GetTickCount-t;

a:=(Image1.Width-w-50)/(t2+1);
w:=round(a*t2);

Image1.Canvas.Rectangle(5, 20, 5+round(a*t1), 20+h);
Image1.Canvas.TextOut(30+w, 20, 'bubbleSort: '+(IntToStr(t1 div k)) +' ms');



Image1.Canvas.Rectangle(5, 40+h, 5+round(a*t2), 40+2*h);
Image1.Canvas.TextOut(30+w, 40+h, 'advancedBubbleSort: '+IntToStr(t2 div k) +' ms');


t:=GetTickCount;
HeapSort (arr3);
t3:=GetTickCount-t;

Image1.Canvas.Rectangle(5, 60+2*h, 5+round(a*t3), 60+3*h);
Image1.Canvas.TextOut(30+w, 60+2*h, 'heapSort: '+IntToStr(t3 div k) +' ms');


t:=GetTickCount;
ShakerSort (arr4);
t4:=GetTickCount-t;

Image1.Canvas.Rectangle(5, 80+3*h, 5+round(a*t4), 80+4*h);
Image1.Canvas.TextOut(30+w, 80+3*h, 'shakersort: '+IntToStr(t4 div k) +' ms');

t:=GetTickCount;
InsertionSort (arr5);
t5:=GetTickCount-t;

Image1.Canvas.Rectangle(5, 100+4*h, 5+round(a*t5), 100+5*h);
Image1.Canvas.TextOut(30+w, 100+4*h, 'insertionsort: '+IntToStr(t5 div k) +' ms');
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
Image1.Canvas.Rectangle(ClientRect);
k:=k+1;
Label1.Caption:='K='+inttostr(k);
for i:= 0 to N-1 do
begin
arr1[i]:=random(100);
arr2[i]:=arr1[i];
arr3[i]:=arr1[i];
arr4[i]:=arr1[i];
arr5[i]:=arr1[i];
end;
w:=Image1.Canvas.TextWidth('advancedbubblesort: 9999 ms');
h:=Image1.Canvas.TextHeight('bubblesort: 9999 ms');
t:=GetTickCount;
BubbleSort (arr1);
t1:=GetTickCount-t+t1;

t:=GetTickCount;
AdvancedBubbleSort (arr2);
t2:=GetTickCount-t+t2;

a:=(Image1.Width-w-50)/(t2+1);
w:=round(a*t2);

Image1.Canvas.Rectangle(5, 20, 5+round(a*t1), 20+h);
Image1.Canvas.TextOut(30+w, 20, 'bubblesort: '+IntToStr(t1 div k) +' ms');

Image1.Canvas.Rectangle(5, 40+h, 5+round(a*t2), 40+2*h);
Image1.Canvas.TextOut(30+w, 40+h, 'advancedbubblesort: '+IntToStr(t2 div k) +' ms');


t:=GetTickCount;
HeapSort (arr3);
t3:=GetTickCount-t+t3;

Image1.Canvas.Rectangle(5, 60+2*h, 5+round(a*t3), 60+3*h);
Image1.Canvas.TextOut(30+w, 60+2*h, 'heapsort: '+IntToStr(t3 div k) +' ms');


t:=GetTickCount;
ShakerSort (arr4);
t4:=GetTickCount-t+t4;

Image1.Canvas.Rectangle(5, 80+3*h, 5+round(a*t4), 80+4*h);
Image1.Canvas.TextOut(30+w, 80+3*h, 'shakersort: '+IntToStr(t4 div k) +' ms');

t:=GetTickCount;
InsertionSort (arr5);
t5:=GetTickCount-t+t5;

Image1.Canvas.Rectangle(5, 100+4*h, 5+round(a*t5), 100+5*h);
Image1.Canvas.TextOut(30+w, 100+4*h, 'insertionsort: '+IntToStr(t5 div k) +' ms');
end;



end.





